package com.freesky.web.controller.biz.activity;

import java.util.ArrayList;
import java.util.Random;

public class ArrayChecker {

    public static ArrayList<Integer> allInversions(int[] numbers){
        ArrayList<Integer> inversions = new ArrayList<Integer>();
        for(int i=0;i<numbers.length-1;i++){
            for(int j=i+1;j<numbers.length;j++){
                if(numbers[i] > numbers[j]){
                    inversions.add(numbers[i]);
                    inversions.add(numbers[j]);
                }
            }
        }
        return inversions;
    }

    public static int countOccur(ArrayList<Integer> nums,int value){
        int count = 0;
        for(int num : nums){
            if(num == value){
                count ++ ;
            }
        }
        return count;
    }

    public static int valueWithMostInversions(int[] numbers){
        ArrayList<Integer> inversions = allInversions(numbers);
        int a = 0;
        int b = 0;
        for(int num : numbers){
            int count = countOccur(inversions,num);
            if(count > a ){
                a = count;
                b = num;
            }
        }
        return b;
    }


    public static int ballThrow(){
        Random rand = new Random();
        int score = rand.nextInt(5) + 1;
        return score*10;
    }

//    public static int ballThrow(){
//        Random random = new Random();
//        int score = random.nextInt(5) + 1;
//        return (score * 10);
//    }

    public static double averageThrow(int numThrows,int minScore){
        int total = 0;
        double avg = 0.0;
        int times = 0;
        for(int i=1;i<=numThrows;i++){
            int score = ballThrow();
            if(score > minScore) {
                total = total + score;
                times = times + 1;
            }
        }

        if(total > 0){
            avg = total / times ;
        }

        return avg;
    }

    public class SecretWord{

        private String original;

        public SecretWord(String word){

        }
        public String getOriginal(){
            return "";
        }

        public String transformWord(){
            return "";
        }
    }

    public class TopSecretWord extends SecretWord{
        private String word;

        public TopSecretWord(String word){
            super(word);
        }

        @Override
        public String getOriginal(){
            return super.getOriginal();
        }

        @Override
        public String transformWord(){
            word = super.transformWord();
            int length = word.length();
            int halfLength = length / 2;

            if(length%2 == 0){
                return "***" + word.substring(halfLength);
            } else {
                return word.substring(0,halfLength) + "***";
            }
        }

        public boolean checkLength(){
            return word.length()>5;
        }
    }

    private Treasure[][] map;

    public class Treasure{
        public Treasure(){

        }

        public int getGold(){
            return 1;
        }
    }

    public class Location{
        public int getRow(){
            return 1;
        }
        public int getCol(){
            return 1;
        }
    }
    public class TreasureMap{

        public TreasureMap(int r,int c,ArrayList<Location> locs){
            map = new Treasure[r][c];
            for(Location loc : locs){
                int row = loc.getRow();
                int col = loc.getCol();
                map[row][col] = new Treasure();
            }
        }


        public int totalGold(Location start,Location end){
            int totalGold = 0;
            for(int i=start.getRow();i<=end.getRow();i++){
                for(int j=start.getCol();j<=end.getCol();j++){
                    if(map[i][j] != null){
                        totalGold =  totalGold + map[i][j].getGold();
                    }
                }
            }
            return totalGold;
        }

        /*
        public TreasureMap(int r,int c,ArrayList<Location> locs){
            map = new Treasure[r][c];
            for(Location loc : locs){
                int row = loc.getRow();  //行
                int col = loc.getCol(); //列
                map[row][col] = new Treasure();
            }
        }

        public int totalGold(Location start,Location end){
            int totalGold = 0;
            for(int i = start.getRow();i<=end.getRow();i++){
                for(int j= start.getCol();j<=end.getCol();j++){
                    if(map[i][j] !=null){
                        totalGold = totalGold + map[i][j].getGold();
                    }
                }
            }
            return totalGold;
        }*/

    }





}
